package com.itheima.config;

import com.itheima.auth.JwtAuthenticationEntryPoint;
import com.itheima.auth.JwtAuthenticationTokenFilter;
import com.itheima.auth.MyAuthenticationFailureHandler;
import com.itheima.auth.MyAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

//自定义配置权限
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Resource
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Resource
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //主要是对一些静态资源的处理
        web.ignoring().antMatchers("/v2/api-docs",
                "/webjars/**",
                "/swagger-resources/configuration/ui",
                "/swagger-resources",
                "/swagger-resources/configuration/security",
                "/swagger-ui.html",
                "/js/**",
                "/css/**"
        );
    }

    /**
     * 配置授权
     * */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //登入请求
        http.formLogin()
                //登入成功后的具体处理
                .successHandler(myAuthenticationSuccessHandler)
                //登入失败后的具体处理
                .failureHandler(myAuthenticationFailureHandler)
                .and()

        //非登入请求

                // 除了登入外接口请求异常处理
                .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .and()

                //处理授权
            .authorizeRequests()
            //request.getUri uri login.html <====> antMatchers(path)
            .antMatchers("/login*").permitAll()
            //post请求多次的处理方法
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            //剩余所有请求 authenticated(需要登入)
            .anyRequest().authenticated()
            .and()
            //默认的login页面 加了<input type='hidden' name='_csrf' valeu='66661122-----1292'>
            //假如csrf启用的，我们调用login后台登入接口需要把_csrf传递给后台
            // 如果我们不清楚_csrf值得来源我们应该禁用
            .csrf().disable();

        //token校验
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
